﻿Public Class frmUtilityChronometer
    Dim imageFond As Image = My.Resources.Images.btn_chrono2
    Dim langue As String
    Dim textFont As New Font("Tahoma", 12, FontStyle.Regular)
    Dim textBrush As New SolidBrush(Color.Black)
    Dim pct As System.Windows.Forms.PictureBox
    Public heureDebut As DateTime
    Dim strCom As String
    Public TempsDeDepart As New System.DateTime()
    Public TempsDArrive As New System.DateTime()
    Public TempsEcoule As New TimeSpan()
    Public TempsEcouleplus As New TimeSpan()
    Public lastClicked As String

    Private Sub frmUtilityChronometer_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PctStart.Image = imageFond
        PctStop.Image = imageFond
        PctContinue.Image = imageFond
        PctReset.Image = imageFond
        PctSave.Image = imageFond
        lastClicked = "Null"
        lblChrono.Text = "00:00:00"
        TxtCom.Text = "Saisissez un commentaire ici"
        strCom = ""
        FormMain.chronoFonctionnel = False
        ' détection de la langue pour personnaliser le texte des boutons
        'TempsDeDepart = Date.Now
        If Me.Text = "Chronomètre" Then
            langue = "fr"
        Else
            langue = "en"
        End If
    End Sub
    Private Sub PctStart_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PctStart.Paint, PctStop.Paint, PctContinue.Paint, PctReset.Paint, PctSave.Paint
        ' sub pour afficher le texte dans chaque picture box
        Dim texte As String = ""
        Dim pos As Integer
        ' verifier la résolution de l'ecran pour centrer l'ecriture par rapport au picturebox (QVGA: 320 x 240 & VGA: 480 x 640 )

        ' netoyer le champ commentaire
        ' TxtCom.Text = ""
        If Screen.PrimaryScreen.WorkingArea.Height < 321 Then
            pos = 70
        Else
            pos = 100
        End If
        ' connaitre l'objet qui a déclanché l'evenement
        pct = sender
        If langue = "fr" Then
            Select Case pct.Name
                Case "PctStart"
                    texte = "Démarrer"
                Case "PctStop"
                    texte = "Arrêter"
                Case "PctContinue"
                    texte = "Continuer"
                Case "PctReset"
                    texte = "Annuler"
                Case "PctSave"
                    texte = "Sauvegarder"
            End Select
            e.Graphics.DrawString(texte, textFont, textBrush, (pos - (10 * texte.Length / 2)), 5)
        Else
            Select Case pct.Name
                Case "PctStart"
                    texte = "Start"
                Case "PctStop"
                    texte = "Stop"
                Case "PctContinue"
                    texte = "Continue"
                Case "PctReset"
                    texte = "Reset"
                Case "PctSave"
                    texte = "Save"
            End Select
            e.Graphics.DrawString(texte, textFont, textBrush, (pos - (10 * texte.Length / 2)), 5)
        End If

    End Sub

    Private Sub PctStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PctStart.Click
        Timer.Enabled = True
        TempsDeDepart = Date.Now
        heureDebut = Now.TimeOfDay.ToString
        pct = sender
        lastClicked = "PctStart"
        FormMain.chronoFonctionnel = True
    End Sub

    Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer.Tick
        If pct.Name = "PctStart" Or pct.Name = "PctContinue" Or lastClicked = "PctStart" Or lastClicked = "PctContinue" Then
            TempsDArrive = Date.Now
            TempsEcoule = TempsDArrive.Subtract(TempsDeDepart)
            lblChrono.Text = TempsEcoule.ToString
        End If

    End Sub

    Private Sub PctStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PctStop.Click
        TempsEcouleplus = TempsEcoule
        pct = sender
        lblChrono.Text = TempsEcoule.ToString
        Timer.Enabled = False
        FormMain.chronoFonctionnel = False

    End Sub

    Private Sub PctContinue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PctContinue.Click
        If pct.Name <> "PctContinue" Then
            Timer.Enabled = True
            TempsDeDepart = Date.Now - TempsEcouleplus
        End If
        pct = sender
        lastClicked = "PctContinue"
        FormMain.chronoFonctionnel = True
    End Sub

    Private Sub PctReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PctReset.Click
        pct = sender
        Timer.Enabled = False
        lblChrono.Text = "00:00:00"
        FormMain.chronoFonctionnel = True
    End Sub

    Private Sub TxtCom_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtCom.TextChanged
        strCom = TxtCom.Text
    End Sub

    Private Sub Clavier_Click_(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clavier.Click
        '    Me.TxtCom.Focus()
        ClSIP.ChangeState()
        If ClSIP.ipSIP.Enabled = True And Me.TxtCom.Text <> "" Then
            Me.TxtCom.Text = ""
        End If
        Me.TxtCom.Focus()
    End Sub

    Private Sub BtnRetour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRetour.Click

        frmUtilityChoix.Show()
        Me.Hide()
    End Sub

    Private Sub btnRetourMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetourMenu.Click
        FormMain.Show()
        Me.Hide()

    End Sub

    Private Sub PctSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PctSave.Click
        saveChronometerData()
    End Sub


    Public Sub saveChronometerData()
        Dim strChaineSauvegarde As String = ";"
        Dim strRetour As String

        strChaineSauvegarde = DateTime.Now.ToShortDateString & ";" & heureDebut & ";" & TempsEcouleplus.ToString & ";" & strCom
        strRetour = saveChronometer(strChaineSauvegarde)
        If strRetour = "error" Then
            MsgBox("erreur dans la creation du fichier", MsgBoxStyle.Critical, "Pocket eReleve")
        Else
            If langue = "fr" Then
                MsgBox("temps sauvegardé dans :" & vbCrLf & "/My Documents/NS-eReleve/chronometer.txt", MsgBoxStyle.OkOnly, "Pocket eReleve")
            Else
                MsgBox("Elapsed time saved in :" & vbCrLf & "/My Documents/NS-eReleve/chronometer.txt", MsgBoxStyle.OkOnly, "Pocket eReleve")
            End If

        End If
        Timer.Enabled = False
        '   End If
    End Sub


    Private Sub BtnRetourSaisie_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDataShow.Click
        RetourSaisie()
        Me.Hide()
    End Sub
End Class
